package com.ruoyi.school.chapter.controller;

import com.ruoyi.school.chapter.domain.Chapter;
import com.ruoyi.school.chapter.service.IChapterService;
import com.ruoyi.school.course.domain.Course;
import com.ruoyi.school.course.service.ICourseService;
import com.ruoyi.school.knowledge.domain.Knowledge;
import com.ruoyi.school.knowledge.service.IKnowledgeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/school/chapter")
public class StudentChapterController
{

    @Autowired
    ICourseService courseService;

    @Autowired
    IChapterService chapterService;

    @Autowired
    IKnowledgeService knowledgeService;

    private String prefix = "school/chapter";

    @RequestMapping("/student_chapter_main/{id}")
    public String chapterMain(@PathVariable("id") Long courseId, Model m)
    {
        // 课程章节首页
        System.out.println("chapterMain()");

        // 课程信息
        Course course = courseService.selectCourseById(courseId);
        m.addAttribute("course",course);

        return prefix+"/student_chapter_main";
    }

    @RequestMapping("/student_chapter_detail/{id}")
    public String chapterDetail(@PathVariable("id") Long chapterId, Model m)
    {
        System.out.println("chapterDetail()"+chapterId);

        // 1. 章节介绍
        Chapter chapter = chapterService.selectChapterById(chapterId);
        m.addAttribute("chapter",chapter);

        // 2. 子章节列表
        Chapter chapter1 = new Chapter();
        chapter1.setFatherId(chapterId);
        List<Chapter> chapters = chapterService.selectChapterList(chapter1);
        m.addAttribute("chapters",chapters);

        // 3. 相关知识点列表
        Knowledge knowledge = new Knowledge();
        knowledge.setChapterId(chapterId);
        List<Knowledge> knows = knowledgeService.selectKnowledgeList(knowledge);
        m.addAttribute("knows",knows);


        if(knows.size()==1)
        {
            Knowledge know = knows.get(0);
            m.addAttribute("know",know);
            System.out.println(know);
        }

        // 4. 相关下载列表

        // 5. 相关作业列表

        return prefix + "/student_chapter_detail";
    }
}
